package com.osig.ec.cfca;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

public class testCompoundSealAutoPdf {

	public static void main(String[] args) throws Exception {

		try {
			PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url);

			/////////////////////////// 如下为用户1的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
			byte[] handwritingImage1 = IoUtil.read(new FileInputStream("D://testcfca//zhangfei.png"));// 1，个人签名照片
			byte[] handwritingData1 = handwritingImage1;
			byte[] identificationFrontData = IoUtil.read(new FileInputStream("D://testcfca//1.png"));// 2,身份证照片
																										// 资产证明
			byte[] identificationBackData = IoUtil.read(new FileInputStream("D://testcfca//2.png")); // tong
																										// 2
			List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
			list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "2"));// 身份证正面
																										// "2"以附件的形式放到pdf中
																										// "identificationFrontImage.png"代表
																										// pdf中的名字
			list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "2"));// 身份证反面
			String proofHashAndMultiData1[] = PaperlessUtil.getProofHashAndMultiData(list1);

			SignLocation signLocation = new SignLocation(1, "代理人签名", "R", 30, 0);//
			// SignLocation signLocation2 = new SignLocation(1, "代理人签名", "R",
			// 10, 0);
			List<SignLocation> signLocationList = new ArrayList<SignLocation>();
			// signLocationList.add(signLocation2);

			SealUserInfo sealUserInfo = new SealUserInfo("张三a", "Z", "0003aa22215", "15744659823", "北京", "签合同");
			String proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo,
					signLocation, proofHashAndMultiData1[0]);
			/////////////////////////// 以上为用户1的数据/////////////////////////////////////////////////

			/////////////////////////// 如下为加盖机构印章的策略/////////////////////////////////////////////////
			// 跑批执行以下
			// sealStrategyXml 多个签约方 复制
			signLocation = new SignLocation(1, "银行签章", "D", 20, 20);
			sealUserInfo = new SealUserInfo("张三五", "北京", "签合同");
			String sealCode = "org001";
			String sealPassword = "11111111";

			String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo,
					signLocation);
			// sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode,
			// sealPassword, handwritingImage1, sealUserInfo, signLocation);

			byte[] pdfFile = IoUtil.read(new FileInputStream("D://testcfca//synthesizeAutoTemplate.pdf"));
			;
			String compoundSealStrategyXml = "<List>" + proofSealStrategyXml1 + sealStrategyXml + "</List>";
			String outputFilePath = "auto";
			// outputFilePath = "/clsu/pdf/pdf2/" + savedPdfId + ".pdf";
			String multiData = "[" + proofHashAndMultiData1[1] + "]";
			// multiData = "";
			String bizSerialNo = GUID.generateId();// 合同编号
			String bizTypeCode = "";
			PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizSerialNo, outputFilePath,
					"6227000015894784559");
			pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizSerialNo, bizTypeCode, outputFilePath);
			pdfBean.setPdfOwnerPassword(Base64.toBase64String("123456".getBytes()));
			String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
			long start = new Date().getTime();
			String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();

			byte[] sealedPdf = clientBean.compoundSealAutoPdf(pdfBeanXml, multiData, compoundSealStrategyXml,
					PaperlessConfig.operatorCode, channelBeanXml);// 生成盖章以后的电子合同

			System.out.println(sealedPdf.length);
			String errorRsString = PaperlessUtil.isError(sealedPdf);
			if ("".equals(errorRsString)) {
				IoUtil.write("D://testcfca//output/compoundSealAutoPdf.pdf", sealedPdf);
			} else {
				System.out.println(new String(sealedPdf, "UTF-8"));
			}
			System.out.println(
					"########## compoundSealAutoPdf" + " end,time:" + (new Date().getTime() - start) + "##########");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
